package cn.mw.cmdb.entity.modelRelation;

import cn.mw.cmdb.entity.ModelInfo;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class ModelRelationView {

    private List nodes = new ArrayList();
    private List<ModelRelationEdge> edges = new ArrayList<>();

    public void extractFrom(ModelAsset modelAsset, List<RelationGroupDTO> groupList, ModelInfo modelInfo) {
        //设置模型信息
        String showId = "model" + modelInfo.getId();
        ModelRelationNode modelRelationNode = new ModelRelationNode();
        modelRelationNode.setId(showId);
        modelRelationNode.setRealModelId(modelInfo.getId());
        modelRelationNode.setImg(modelInfo.getIcon());
        modelRelationNode.setLabel(modelInfo.getModelName());
        nodes.add(modelRelationNode);

        //关系信息按分组归类
        Map<String, List<ModelRelationDTO>> groupMap = new HashMap<>();

        List<ModelRelate> allModelRelate = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(modelAsset.getOutModelRelates())) {
            allModelRelate.addAll(modelAsset.getOutModelRelates());
        }

        if (CollectionUtils.isNotEmpty(modelAsset.getInModelRelates())) {
            allModelRelate.addAll(modelAsset.getInModelRelates());
        }

        if (modelAsset != null && CollectionUtils.isNotEmpty(allModelRelate)) {
            ModelRelationDTO modelRelationDTO = null;
            for (ModelRelate modelRelate : allModelRelate) {
                modelRelationDTO = modelRelate.getModelRelationDTO();
                if (null != modelRelationDTO) {
                    for (RelationGroupDTO groupData : groupList) {
                        String groupId = groupData.getId();
                        ModelGroupParam modelGroupParam = new ModelGroupParam(modelInfo.getId(), groupId);

                        //判断关系是否属于该分组
                        if (modelRelationDTO.isBelongGroup(modelGroupParam)) {
                            List<ModelRelationDTO> dataList = groupMap.get(modelGroupParam.getGroupId());
                            if (null == dataList) {
                                dataList = new ArrayList<>();
                                groupMap.put(modelGroupParam.getGroupId(), dataList);
                            }
                            dataList.add(modelRelationDTO);
                        }
                    }
                }
            }
        }

        for (RelationGroupDTO data : groupList) {
            String groupId = data.getId();
            Boolean defautGroupFlag = data.isDefautGroupFlag();
            List<ModelRelationDTO> relationDTOS = groupMap.get(groupId);

            //用户新建分组时,此时没有关系,需要显示分组
            if (null != relationDTOS) {
                doAddGroupInfo(data, relationDTOS, modelInfo.getId(), showId);
            } else if (!defautGroupFlag) {
                doAddGroupInfo(data, relationDTOS, modelInfo.getId(), showId);
            }
        }
    }

    private void doAddGroupInfo(RelationGroupDTO data , List<ModelRelationDTO> relationDTOS ,String ownModelId ,String showId){
        ModelRelationGroupInfo modelRelationGroupInfo = new ModelRelationGroupInfo();
        modelRelationGroupInfo.extractFrom(data ,relationDTOS ,ownModelId);
        nodes.add(modelRelationGroupInfo);

        ModelRelationEdge edge = new ModelRelationEdge();
        edge.setSource(showId);
        edge.setTarget(modelRelationGroupInfo.getId());
        edges.add(edge);
    }
}
